<?php
/**
* ==========================================================
* Code Igniter User Authentication Class
*
* Original Back-end userauth code by Craig Rodway, 
* craig dot rodway at gmail dot com
*
* Front end, transition to models and code enhancement 
* Derek Allard, derek at darkhorse dot to
*
* PHP 4 backwards compatibility and some bug stomping, 
* View display adapted into mini-app template
* George Dunlop, peccavio at peccavi dot com
*
* September 1, 2006 added load language file
* to CI standard language implementation.
* Thomas Traub tomcode.com
*
* ==========================================================
*/
class Usergroups extends Controller {

	function Usergroups()
	{
		parent::Controller();
		// userauth check for admin role with return uri
		$this->authorize->roleCheck('admin', $this->uri->uri_string());
		$this->lang->load('userauth', $this->session->userdata('ua_language'));
		$this->lang->load('validation', $this->session->userdata('ua_language'));
	}

	function index()
	{
		$ua_data['listAllUsers'] = $this->user_group_model->listAllInfo();
		$ua_data['listAllGroups'] = $this->user_group_model->listUG('groups');
		if ($this->session->flashdata('msg')) {
			$ua_data['msg'] = $this->session->flashdata('msg');
		}
		if ($this->session->flashdata('groupmsg')) {
			$ua_data['groupmsg'] = $this->session->flashdata('groupmsg');
		}
		// this is just db cleanup.  Remove old "remember me" cookie references from the db
		// this model is not actually finished yet, so it currently does nothing
		$this->remember_me->removeOldRememberMe();
		
		// get data view to template
		$data['nav'] = 'users';		// menu steering
		$data['title'] = $this->lang->line('ua_manage_title');
	  $data['extraHeadContent'] = '<script type="text/javascript" src="' . base_url(). 
			'js/effects.js"></script>';
			$data['extraHeadContent'] .= '<script type="text/javascript" src="' . base_url(). 
			'js/userauth.js"></script>';
		$data['extraHeadContent'] .= '<link type="text/css" rel="stylesheet" href="' . base_url().
			'/css/userauth.css" />';
		$data['content'] = $this->load->view('usergroups/userauth', $ua_data, TRUE);

		// Display in Template
		$this->load->view('template', $data);
	}

	function addUser ()
	{
		$this->load->library('validation');
		$ua_data['listAllGroups'] = $this->user_group_model->listUG('groups');
		$rules['nomo'] = "required|max_length[16]|xss_clean";
		$rules['grup_id'] = "required|numeric";
		$rules['retposxto'] = "required|max_length[100]|valid_email";
		$rules['pasvorto'] = "matches[passconf]|max_length[40]";
		$rules['passconf'] = "required";
		$rules['cxuaktiva'] = "required|numeric";
		$this->validation->set_rules($rules);
		$fields['nomo'] = $this->lang->line('ua_username');
		$fields['grup_id'] = $this->lang->line('ua_group');
		$fields['retposxto'] = $this->lang->line('ua_email');
		$fields['pasvorto'] = $this->lang->line('ua_password');
		$fields['passconf'] = $this->lang->line('ua_passconf');
		$fields['cxuaktiva'] = $this->lang->line('ua_status');
		$this->validation->set_fields($fields);
		$this->validation->set_error_delimiters('<span class="error">', '</span>');

		if ($this->validation->run() == TRUE) {
			$username = $this->input->post('nomo', TRUE);
			$password = $this->input->post('pasvorto', TRUE);
			$email    = $this->input->post('retposxto', TRUE);
			$enabled  = $this->input->post('cxuaktiva', TRUE);
			$group    = $this->input->post('grup_id', TRUE);
	
			$status = $this->user_group_model->addUser($username, $password, 
											$email, $enabled, $group);
			if ($status == 0) {
				$ua_data['msg'] = $this->lang->line('ua_missing');
			} elseif ($status == 1) {
				$this->session->set_flashdata('msg', $this->lang->line('ua_user_added'));
				redirect('admin/usergroups', 'location');	
			} else {
				$ua_data['msg'] = $this->lang->line('ua_user_exists');
			}
		}
		$ua_data['mode'] = 'add';
		$data['nav'] = 'adduser';
		$ua_data['form_title'] = $data['title'] = $this->lang->line('ua_adduser');
		$data['extraHeadContent'] = '<link type="text/css" rel="stylesheet" href="' . base_url(). 
			'/css/userauth.css" />';
		$data['content'] = $this->load->view('usergroups/user_form', $ua_data, TRUE);

		// Display in Template
		$this->load->view('template', $data);
	}

	function editUser ()
	{
		$this->load->library('validation');
		$userid = (int) $this->uri->segment(4, $this->input->post('uz_id', TRUE));
		$ua_data['uz_id'] = $userid;
		$ua_data['userinfo'] = $this->user_group_model->listUserInfo($userid);
		$ua_data['currentgroup'] = $ua_data['userinfo']->grup_id;
		$ua_data['listAllGroups'] = $this->user_group_model->listUG('groups');
		$rules['nomo'] = "required|max_length[16]|xss_clean";
		$rules['grup_id'] = "required|numeric";
		$rules['retposxto'] = "required|max_length[100]|valid_email";
		$rules['pasvorto'] = "matches[passconf]|max_length[40]";
		$rules['cxuaktiva'] = "required|numeric";
		$this->validation->set_rules($rules);
		$fields['nomo'] = $this->lang->line('ua_username');
		$fields['grup_id'] = $this->lang->line('ua_group');
		$fields['retposxto'] = $this->lang->line('ua_email');
		$fields['pasvorto'] = $this->lang->line('ua_password');
		$fields['cxuaktiva'] = $this->lang->line('ua_status');
		$this->validation->set_fields($fields);
		$this->validation->set_error_delimiters('<span class="error">', '</span>');

		if ($this->validation->run() == TRUE) {
			$userid   = $this->input->post('uz_id', TRUE);
			$username = $this->input->post('nomo', TRUE);
			$password = $this->input->post('pasvorto', TRUE);
			$email    = $this->input->post('retposxto', TRUE);
			$enabled  = $this->input->post('cxuaktiva', TRUE);
			$group    = $this->input->post('grup_id', TRUE);

			$status   = $this->user_group_model->editUser ($userid, $username, 
								$password, $email, $enabled, $group);
			if ($status) {
				$this->session->set_flashdata('msg', $this->lang->line('ua_user_edited'));
				redirect('admin/usergroups', 'location');	
			} else {
				$ua_data['msg'] = $this->lang->line('ua_missing');
			}
		}
		$ua_data['mode'] = 'edit';
		$data['nav'] = 'edituser';
		$ua_data['form_title'] = $data['title'] = $this->lang->line('ua_edituser');
		$data['extraHeadContent'] = '<link type="text/css" rel="stylesheet" href="' . base_url(). 
			'/css/userauth.css" />';
		$data['content'] = $this->load->view('usergroups/user_form', $ua_data, TRUE);

		// Display in Template
		$this->load->view('template', $data);
	}
	
	function removeUser ()
	{
		$userid = (int) $this->uri->segment(4);
		$username = $this->user_group_model->getUserName($userid);
		if ($this->user_group_model->removeUser($username)) {
			$this->session->set_flashdata('msg', $this->lang->line('ua_user_removed'));
			redirect('admin/usergroups');
		} else {
			$this->session->set_flashdata('msg', $this->lang->line('ua_user_not_removed'));
			redirect('admin/usergroups');
		}
	}
	
	function addGroup ()
	{
		$this->load->library('validation');
		$rules['grupnomo'] = "required|max_length[50]|xss_clean";
		$rules['priskribo'] = "required|max_length[255]|xss_clean";
		$this->validation->set_rules($rules);
		$fields['grupnomo'] = $this->lang->line('ua_groupname');
		$fields['priskribo'] = $this->lang->line('ua_groupdescription');
		$this->validation->set_fields($fields);
		$this->validation->set_error_delimiters('<span class="error">', '</span>');

		if ($this->validation->run() == TRUE) {
			$groupname = $this->input->post('grupnomo', TRUE);
			$groupdescription = $this->input->post('priskribo', TRUE);
	
			$status = $this->user_group_model->addGroup($groupname, $groupdescription);

			if ($status) {
				$this->session->set_flashdata('groupmsg', $this->lang->line('ua_group_added'));
				redirect('admin/usergroups', 'location');	
			} else {
				$this->session->set_flashdata('groupmsg', $this->lang->line('ua_group_added_err'));
				redirect('admin/usergroups', 'location');	
			}
		}
		$ua_data['mode'] = 'add';
		$data['nav'] = 'addgroup';
		$ua_data['form_title'] = $data['title'] = $this->lang->line('ua_addgroup');
		$data['extraHeadContent'] = '<link type="text/css" rel="stylesheet" href="' . base_url(). 
			'/css/userauth.css" />';
		$data['content'] = $this->load->view('usergroups/group_form', $ua_data, TRUE);

		// Display in Template
		$this->load->view('template', $data);
	}

	function editGroup ()
	{
		$this->load->library('validation');
		$groupid = (int) $this->uri->segment(4, $this->input->post('grup_id', TRUE));
		$ua_data['grup_id'] = $groupid;
		$ua_data['groupinfo'] = $this->user_group_model->listGroupInfo($groupid);
		$rules['grupnomo'] = "required|max_length[50]|xss_clean";
		$rules['priskribo'] = "required|max_length[255]|xss_clean";
		$this->validation->set_rules($rules);
		$fields['grupnomo'] = $this->lang->line('ua_groupname');
		$fields['priskribo'] = $this->lang->line('ua_groupdescription');
		$this->validation->set_fields($fields);
		$this->validation->set_error_delimiters('<span class="error">', '</span>');
		
		if ($this->validation->run() == TRUE) {
			$groupname = $this->input->post('grupnomo', TRUE);
			$groupdescription = $this->input->post('priskribo', TRUE);
	
			$status = $this->user_group_model->editGroup($groupid, $groupname, $groupdescription);
			if ($status) {
				$this->session->set_flashdata('groupmsg', $this->lang->line('ua_group_edited'));
				redirect('admin/usergroups', 'location');	
			} else {
				$this->session->set_flashdata('groupmsg', $this->lang->line('ua_group_edited_err'));
				redirect('admin/usergroups', 'location');	
			}
		}
		$ua_data['mode'] = 'edit';
		$data['nav'] = 'editgroup';
		$ua_data['form_title'] = $data['title'] = $this->lang->line('ua_editgroup');
		$data['extraHeadContent'] = '<link type="text/css" rel="stylesheet" href="' . base_url(). 
			'/css/userauth.css" />';
		$data['content'] = $this->load->view('usergroups/group_form', $ua_data, TRUE);

		// Display in Template
		$this->load->view('template', $data);
	}

	function removeGroup ()
	{
		$status = $this->user_group_model->removeGroup((int) $this->uri->segment(4));
		if ($status == 0) {
			$this->session->set_flashdata('groupmsg', $this->lang->line('ua_removal_err'));
			redirect('admin/usergroups', 'location');
		} elseif ($status == 1) {
			$this->session->set_flashdata('groupmsg', $this->lang->line('ua_group_removed'));
			redirect('admin/usergroups', 'location');
		} else {
			$this->session->set_flashdata('groupmsg', $this->lang->line('ua_removal_err_members'));
			redirect('admin/usergroups', 'location');
		}
	}
}

?>